package com.ad.pet.app.shopcar.adapter;

import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ad.pet.R;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.data.utils.glide.GlideUtil;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.cart.Good;
import com.ad.pet.models.remote.mall.SkuProps;
import com.ad.pet.models.remote.mall.Skumap;
import com.ad.pet.ui.MyViewHolder;

import java.util.List;

/**
 * Created by SEELE on 2018/4/18.
 */

public class GoodAdapter extends BaseQuickAdapter<Good, MyViewHolder> {
    private OnSelectClickLinstener mOnSelectClickLinstener;
    private OnChoseAttributeLinstener mOnChoseAttributeLinstener;

    public GoodAdapter(@Nullable List<Good> data) {
        super(R.layout.item_cart_layout, data);
    }

    @Override
    protected void convert(final MyViewHolder helper, final Good item) {

        helper.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                remove(helper.getLayoutPosition());
            }
        });
        final TextView tvNumber = helper.getView(R.id.tv_number);
        tvNumber.setText(String.valueOf(item.getQuantity()));
        ImageView ivChose = helper.getView(R.id.iv_check);
        ImageView ivGood = helper.getView(R.id.iv_good);
        TextView tvGoodName = helper.getView(R.id.tv_title);
        final TextView tvAttribute = helper.getView(R.id.tv_attribute);
        TextView tvTips = helper.getView(R.id.tv_tips);
        TextView tv_old_price = helper.getView(R.id.tv_old_price);
        tvAttribute.setVisibility(TextUtils.isEmpty(item.getAttr_option()) ? View.GONE : View.VISIBLE);
        TextView ivRemove = helper.getView(R.id.tv_remove);
        TextView ivAdd = helper.getView(R.id.tv_add);
        ivChose.setImageResource(item.isChose() ? R.drawable.ic_chose : R.drawable.ic_unchose);
        ivChose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mData.get(helper.getLayoutPosition()).setChose(!mData.get(helper.getLayoutPosition()).isChose());
                if (mOnSelectClickLinstener != null) {
                    mOnSelectClickLinstener.onSelectClickLinstener(helper.getLayoutPosition());
                }
                notifyDataSetChanged();
            }
        });
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnSelectClickLinstener != null) {
                    if (item.getQuantity() <= 150) {
                        item.setQuantity(item.getQuantity() + 1);
                        mOnSelectClickLinstener.onAddNumber(helper.getLayoutPosition(), item.getQuantity());
                        notifyDataSetChanged();
                    } else {
                        item.setQuantity(150);
                        ToastUtils.showLongToast(mContext.getString(R.string.than_150_tips));
                    }
                }
            }
        });
        ivRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnSelectClickLinstener != null) {
                    if (item.getQuantity() > 1) {
                        item.setQuantity(item.getQuantity() - 1);
                        mOnSelectClickLinstener.onRemoveNumber(helper.getLayoutPosition(), item.getQuantity());
                        notifyDataSetChanged();
                    }
                }
            }
        });

        tvAttribute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnChoseAttributeLinstener != null) {
                    mOnChoseAttributeLinstener.onChoseAttribute(item.getSkuProps(), item.getSkuMap(), helper.getLayoutPosition(), tvAttribute.getText().toString());
                }
            }
        });


        TextView tvPrice = helper.getView(R.id.tv_price);

        tvPrice.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + item.getPrice());

        tv_old_price.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + item.getOrig_price());
        tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        GlideUtil.load(mContext, item.getImage(), ivGood);
        tvGoodName.setText(item.getName());
        tvAttribute.setText((TextUtils.isEmpty(item.getAttr_option()) ? "" : item.getAttr_option()));

        tvTips.setVisibility(TextUtils.isEmpty(item.getPrice_reduced()) ? View.GONE : View.VISIBLE);
        tvTips.setText(item.getPrice_reduced());

        tvNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSelectClickLinstener != null) {
//                    if (item.getFree_shipping_99() == 1) {
//                        ToastUtil.getToastUtil().showShort(mContext.getString(R.string.count99_tips));
//                        return;
//                    }
//                    DialogUtil.createDialog(((Activity) mContext), mContext.getString(R.string.input_buy_number), new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            AlertDialog alertDialog = (AlertDialog) dialog;
//                            EditText editText = alertDialog.findViewById(R.id.et_say);
//                            if (TextUtils.isEmpty(editText.getText()) || Integer.valueOf(editText.getText().toString()) == item.getNumber()) {
//                                return;
//                            }
//                            item.setNumber(Integer.valueOf(editText.getText().toString()));
//                            mOnSelectClickLinstener.onSetNumber(helper.getLayoutPosition(), item.getNumber());
//                            notifyDataSetChanged();
//                        }
//                    }, String.valueOf(item.getNumber()));
                }
            }
        });

        if (item.getQuantity() <= 1) {
            ivRemove.setTextColor(mContext.getResources().getColor(R.color.graycf));
        } else {
            ivRemove.setTextColor(mContext.getResources().getColor(R.color.gray34));
        }
    }

    public interface OnSelectClickLinstener {
        void onSelectClickLinstener(int position);

        void onRemoveNumber(int position, int number);

        void onAddNumber(int position, int number);

        void onSetNumber(int position, int number);
    }


    public void setOnSelectClickLinstener(OnSelectClickLinstener onSelectClickLinstener) {
        mOnSelectClickLinstener = onSelectClickLinstener;
    }

    public void setAllChose(boolean ischose) {
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setChose(ischose);
        }
        notifyDataSetChanged();
    }


    public interface OnChoseAttributeLinstener {
        void onChoseAttribute(List<SkuProps> skuProps, List<Skumap> skuMap, int position, String sku);
    }

    /**
     * 設置點擊修改屬性的接口
     *
     * @param onChoseAttributeLinstener
     */
    public void setOnChoseAttributeLinstener(OnChoseAttributeLinstener onChoseAttributeLinstener) {
        mOnChoseAttributeLinstener = onChoseAttributeLinstener;
    }
}
